/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package merlion.common.entity;

import com.sun.jersey.core.util.Base64;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import merlion.common.util.Consts;
import merlion.common.util.CryptographicHelper;

/**
 *
 * @author Zhang Ying nd Zhiqi
 */
@Entity
public class SystemUserAccount implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String username;
    private String status;
    private String emailAddress;
    private String encryptedPassword;
    private Timestamp lastLoginTime;
    private String question1;
    private String answer1;
    private String token;
    
    // for common usage:
    @OneToOne(mappedBy = "systemUserAccount", targetEntity = Staff.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    private Staff staff;
    
    @OneToMany(targetEntity=Message.class, cascade={CascadeType.ALL}, fetch= FetchType.EAGER)
    private Collection<Message> messages = new ArrayList<Message>();
    
    @OneToMany(targetEntity=AuditLog.class, cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    private Collection<AuditLog> auditLogs = new ArrayList<AuditLog>();
    
    @OneToMany(targetEntity=JobToDo.class, cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    private Collection<JobToDo> jobToDos = new ArrayList<JobToDo>();

    public SystemUserAccount() {
    }

    public void create(String status, String emailAddress, String encryptedPassword, String username, Timestamp lastLoginTime, String qns, String ans) {
        this.setStatus(status);
        this.setEmailAddress(emailAddress);
        this.setEncryptedPassword(encryptedPassword);
        this.setUsername(username);
        this.setLastLoginTime(lastLoginTime);
        this.setAnswer1(ans);
        this.setQuestion1(qns);
        CryptographicHelper cryptographicHelper = new CryptographicHelper();
        if (status.equals(Consts.ACCOUNT_ACTIVATED)) {
            this.setToken(null);
        } else {
            this.setToken(Base64.encode(cryptographicHelper.doMD5Hashing(username + "." + emailAddress)).toString());
        }
    }

    public Staff getStaff() {
        return staff;
    }

    public void setStaff(Staff staff) {
        this.staff = staff;
    }

    public String getAnswer1() {
        return answer1;
    }

    public void setAnswer1(String answer1) {
        this.answer1 = answer1;
    }

    public String getQuestion1() {
        return question1;
    }

    public void setQuestion1(String question1) {
        this.question1 = question1;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        CryptographicHelper cryptographicHelper = new CryptographicHelper();
        this.username = username;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        CryptographicHelper cryptographicHelper = new CryptographicHelper();
        this.emailAddress = emailAddress;
    }

    public String getEncryptedPassword() {
        return encryptedPassword;
    }

    public void setEncryptedPassword(String encryptedPassword) {
        CryptographicHelper cryptographicHelper = new CryptographicHelper();
        String encryptedPasswordTemp = cryptographicHelper.doAESEncryptString(encryptedPassword,
                cryptographicHelper.getDefaultEncryptionKey(), cryptographicHelper.getDefaultEncryptionIV());
        this.encryptedPassword = encryptedPasswordTemp;
    }

    public Timestamp getLastLoginTime() {
        return lastLoginTime;
    }

    public void setLastLoginTime(Timestamp lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Collection<Message> getMessages() {
        return messages;
    }

    public void setMessages(Collection<Message> messages) {
        this.messages = messages;
    }

    public Collection<AuditLog> getAuditLogs() {
        return auditLogs;
    }

    public void setAuditLogs(Collection<AuditLog> auditLogs) {
        this.auditLogs = auditLogs;
    }

    public Collection<JobToDo> getJobToDos() {
        return jobToDos;
    }

    public void setJobToDos(Collection<JobToDo> jobToDos) {
        this.jobToDos = jobToDos;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof SystemUserAccount)) {
            return false;
        }
        SystemUserAccount other = (SystemUserAccount) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "merlion.common.entity.SystemUserAccount[ id=" + id + " ]";
    }
}
